package com.imu.wys.aop;

import com.imu.wys.model.User;
import com.imu.wys.service.UserService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.Method;

@Component
@Aspect
public class ControllerAspect {
    @Autowired
    UserService userService;

    @AfterReturning(value = "execution(* com.imu.wys.controller.UserController.*(..))",returning = "result")
    public void afterReturning(JoinPoint point,Object result) {
        String msg = (String)((ModelAndView) result).getModel().get("msg");
        User user = (User) ((ModelAndView) result).getModel().get("user");
        String methodName = point.getSignature().getName();
        System.out.println(methodName);
        if(user!=null){//操作成功
            if(methodName.equals("login")){
                userService.insertUserLog(user,"登录成功");
            }else if (methodName.equals("register")) {
                userService.insertUserLog(user,"注册成功");
            }

        }else {//登录失败
            if(methodName.equals("login")){
                Object[] args = point.getArgs();
                User user1 = new User();
                user1.setUsername(args[1].toString());
                userService.insertUserLog(user1,"登录失败");
            }
        }
    }
}
